package com.lx.demo;

import com.lx.demo.entity.Person;
import com.lx.demo.entity.Student;
import com.lx.demo.entity.Teacher;

import java.util.Scanner;

/**
 * @author c先生
 * @date 2022年03月21日 下午 2:55:42
 * @describe
 */

public class Util {

    static Person[] personStr = new Person[20];
    Scanner sc = new Scanner(System.in);

    //初始化数组
    static{
        Student stu = new Student();
        stu.setClasses("T6");
        stu.setCardId("123");
        stu.setName("李立");
        stu.setGender("男");
        stu.setHomeTown("河南南阳");

        Student student1 = new Student();
        student1.setCardId("456");
        student1.setName("李欣");
        student1.setGender("男");
        student1.setHomeTown("河南南阳");
        student1.setClasses("T09");

        Student student2 = new Student();
        student2.setCardId("456");
        student2.setName("李欣");
        student2.setGender("男");
        student2.setHomeTown("河南南阳");
        student2.setClasses("T09");

        Teacher teacher = new Teacher();
        teacher.setCardId("101");
        teacher.setName("林白村");
        teacher.setGender("女");
        teacher.setHomeTown("北京海淀");
        teacher.setLevel(10);
        personStr[0]=stu; //向上转型
        personStr[1]=student1;//向上转型
        personStr[2]=student2;//向上转型
        personStr[3]=teacher;
    }

    //展示所有人员信息
    public void showAll(){
        for (Person person : personStr) {
            if(person != null) {
                person.showMe();
            }
        }
    }
    //根据身份证号判断是否已存在此人
    public boolean findPersonExistsByCardId(String carId){
        for (int i = 0; i < personStr.length; i++) {
            if(personStr[i] != null && personStr[i].getCardId().equals(carId)){
                //存在，不能添加。
                return false;
            }
        }
        //不存在，可以添加。
        return true;
    }
    //添加一名人员信息
    public boolean addPerson(Person person){
        for (int i = 0; i < personStr.length; i++) {
            if(personStr[i] == null){
                personStr[i] = person;
                return true;
            }
        }
        return false;
    }

    //添加信息
    public Person inputUpdateOrAddMessage(String carld){
        System.out.println("请输入人口信息的姓名");
        String name = sc.next();
        System.out.println("请输入人口信息的性别");
        String gender = sc.next();
        System.out.println("请输入人口信息的籍贯");
        String homeTown = sc.next();
        System.out.println("请输入人口的职业(学生/老师)");
        String key = sc.next();
        Person person = new Person();
        switch (key){
            case "学生":
                Student student = new Student();
                System.out.println("请输入班级：");
                student.setClasses(sc.next());
                person = student;
                break;
            case "老师":
                Teacher teacher = new Teacher();
                System.out.println("请输入职场等级：");
                teacher.setLevel(sc.nextInt());
                person = teacher;
                break;
            default:
                break;
        }
        person.addFild(carld,name,gender,homeTown);
        return person;
    }

    //根据身份证号删除人员信息
    public boolean deletePerson(String carId){
        for (int i = 0; i < personStr.length; i++) {
            if(personStr[i]!=null && personStr[i].getCardId().equals(carId)){
                personStr[i]=null;
                return true;
            }
        }
        return false;
    }

    //根据身份证号修改人员信息
    public boolean updatePerson(Person person){
        for (int i = 0; i < personStr.length; i++) {
            if(personStr[i] != null){
                personStr[i] = person;
                return true;
            }
        }
        return false;
    }
}
